const router = require('koa-router')()
const Mock = require('mockjs');


let { list } = Mock.mock({
  // 标题, 图片, 描述， id等字段
  "list|10000": [{
    "id": "@id",
    "title": "@ctitle(5, 10)",
    "image": "@image('100x100', '#50B347', '#FFF', 'Hello', 'Hello Koa')",
    "desc": "@csentence(10, 20)",
    "time": "@datetime",
    "price|1-1000": 1,
    "type|1": ["a", 'b', 'c', 'd']
  }]
})
function textFilter(params, value, list) {
  if (!value) return list
  return list.filter(v => v[params].includes(value))
}

function durationFilter(params, start, end, list) {
  if (!start && !end) {
    return list
  }
  if (start != undefined && end != undefined) {
    // start < v < end
    return list.filter(v => {
      if(params === 'time') {
        const a = new Date(v[params]).getTime()
        const b = new Date(start).getTime()
        const c = new Date(end).getTime()
        return a >= b && a <= c
      }
      return v[params] >= start && v[params] <= end
    })
  }

  if (start === undefined && end != undefined) {
    // v < end
    return list.filter(v => {
      if(params === 'time') {
        const a = new Date(v[params]).getTime()
        const b = new Date(end).getTime()
        return a <= b
      }
      return v[params] <= end
    })
  }

  if (start != undefined && end === undefined) {
    // start < v
    return list.filter(v => {
      if(params === 'time') {
        const a = new Date(v[params]).getTime()
        const b = new Date(start).getTime()
        return a >= b
      }

      return v[params] >= start
    })
  }
}
router.get('/list', ctx => {
  let newList = list
  const { id, title, desc, startTime, endTime, startPrice, endPrice, type } = ctx.request.query;
  // 查询
  // id, 
  newList = textFilter('id', id, newList)
  // 标题
  newList = textFilter('title', title, newList)
  // 描述
  newList = textFilter('desc', desc, newList)
  // 时间段
  newList = durationFilter('time', startTime, endTime, newList)
  // 价格区间
  newList = durationFilter('price', startPrice, endPrice, newList)
  // 类型
  newList = textFilter('type', type, newList)
  // 返回
  ctx.body = {
    code: 200,
    data: newList
  }
})

// 列表数据接口
router.get('/list/echarts/bar', ctx => {
  // 把数据list进行分类， 根据单价price
  // 分类 1-200, 200-400, 400-600, 600-800, 800-1000
  // 注意， 我只要每个数据分类的总个数
  const result = {}
  list.forEach(v => {
    if (v.price < 200) {
      result['1-200'] = result['1-200'] ? result['1-200'] + 1 : 1
    }
    if (v.price >= 200 && v.price < 400) {
      result['200-400'] = result['200-400'] ? result['200-400'] + 1 : 1
    }
    if (v.price >= 400 && v.price < 600) {
      result['400-600'] = result['400-600'] ? result['400-600'] + 1 : 1
    }
    if (v.price >= 600 && v.price < 800) {
      result['600-800'] = result['600-800'] ? result['600-800'] + 1 : 1
    }
    if (v.price >= 800 && v.price < 1000) {
      result['800-1000'] = result['800-1000'] ? result['800-1000'] + 1 : 1
    }
  })

  ctx.body = {
    code: 200,
    data: result
  }
})

// 列表删除
router.post('/list/delete', ctx => {
  const { id } = ctx.request.body;
  // 这里直接执行通过id删除list中项目
  list = list.filter(item => !id.includes(item.id));
  // 返回
  ctx.body = {
    code: 200,
    data: list,
    message: '删除成功'
  }
})

// 列表添加
router.post('/list/add', ctx => {
  list.unshift({
    ...ctx.request.body,
    id: Mock.Random.id(),
  });
  ctx.body = {
    code: 200,
    data: list,
    message: '添加成功'
  }
})


module.exports = router


